/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 *
 */

import path from 'path';

import { cloneDeep } from '@baize/wdk';
import { hvigorCore } from '@ohos/hvigor';
import { ENABLE_OVERRIDES_DEPENDENCY_MAP } from '@ohos/hvigor/src/common/const/const.js';
import { HVIGOR_PROJECT_ROOT_DIR } from '@ohos/hvigor/src/common/const/path-const';

import { CommonConst } from '../../../const/common-const.js';
import { getJson5Obj } from '../../json-util.js';
import { configFileLoader } from '../config-file-loader.js';


// 这个类是为了解决overrideDependencyMap适配问题
// 保证与现网版本行为一致
class OhModulesLockLoader {
  // lock.json5文件路径
  private lockFilePath = path.resolve(HVIGOR_PROJECT_ROOT_DIR, CommonConst.OH_MODULES, CommonConst.OHPM, CommonConst.LOCK_JSON5);


  /**
   * 获取lock.json5文件对象
   */
  private getLockJsonObj() {
    let lockJsonObj = configFileLoader.getConfigFileJson(this.lockFilePath);
    if (lockJsonObj === undefined) {
      configFileLoader.setConfigFileJson(this.lockFilePath, getJson5Obj(this.lockFilePath));
      lockJsonObj = configFileLoader.getConfigFileJson(this.lockFilePath);
    }
    return lockJsonObj ?? {};
  }

  /**
   * 获得被复写的模块名
   * 通过工程级的oh-package的overrideDependencyMap的配置来筛选被复写的模块
   */
  private isModuleInProjectPkgJsonOverrideDependencies(moduleName: string): boolean {
    const projectPkgJsonPath = path.resolve(HVIGOR_PROJECT_ROOT_DIR, CommonConst.OH_PACKAGE_JSON5);
    const projectPkgJsonObj = configFileLoader.getConfigFileJson(projectPkgJsonPath) ?? {};

    // 获取工程级的oh-package的overrideDependencyMap的配置的key
    return !!projectPkgJsonObj.overrideDependencyMap?.[moduleName];
  }

  /**
   * 判断模块是否需要被改写
   * 需要hvigor-config.json5中的properties中开关enableOverridesDependencyMap打开
   * 以及在项目级oh-package.json5的overrideDependencyMap中配置该模块名
   *
   * @param moduleName 模块名
   * @returns boolean 是否被覆盖
   */
  public isNeedOverrideWithLockJson(moduleName: string): boolean {
    return hvigorCore.getParameter().getProperty(ENABLE_OVERRIDES_DEPENDENCY_MAP) === true
        && this.isModuleInProjectPkgJsonOverrideDependencies(moduleName);
  }


  /**
   * 提取 module的overrideDependencyMap的依赖配置
   * 通过读取lock.json5文件来获取配置
   *
   * @param moduleName 需要被
   * @param pkgObj 返回的是克隆的对象
   */
  public getOverrideDependencyPkgObj(moduleName: string, pkgObj: any) {
    // 提取 module 的覆盖依赖
    // deepClone 一个pkgObj的对象返回克隆的对象
    const overrideDependencyPkgObj = cloneDeep(pkgObj);
    const lockObj = this.getLockJsonObj();
    const overrideDependencies = lockObj.overrideDependencyMap?.[moduleName];
    if (overrideDependencyPkgObj && overrideDependencies) {
      overrideDependencyPkgObj.dependencies = overrideDependencies.dependencies ?? {};
      overrideDependencyPkgObj.devDependencies = overrideDependencies.devDependencies ?? {};
      overrideDependencyPkgObj.dynamicDependencies = overrideDependencies.dynamicDependencies ?? {};
    }

    return overrideDependencyPkgObj;
  }

}


export const ohModulesLockLoader: OhModulesLockLoader = new OhModulesLockLoader();
